unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.TMSBaseControl, FMX.TMSGridCell, FMX.TMSGridOptions, FMX.TMSGridData,
  FMX.TMSCustomGrid, FMX.TMSGrid, FMX.TMSTableView, FMX.TMSWebBrowser,
  FMX.WebBrowser, FMX.TMSDateTimeEdit, FMX.Controls.Presentation, FMX.StdCtrls,
  rtcInfo, rtcConn, rtcDataCli, rtcHttpCli,QJson, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
    Grid_Data: TTMSFMXGrid;
    HttpClient1: TRtcHttpClient;
    Btnx_1: TButton;
    Btnx_2: TButton;
    DataRequest1: TRtcDataRequest;
    IdHTTP1: TIdHTTP;
    Btnx_3: TButton;
    procedure FormShow(Sender: TObject);
    procedure Btnx_1Click(Sender: TObject);
    procedure Grid_DataCanFocus(Sender: TObject; var ACanFocus: Boolean);
    procedure Grid_DataCellClick(Sender: TObject; ACol, ARow: Integer);
    procedure Btnx_2Click(Sender: TObject);
    procedure DataRequest1BeginRequest(Sender: TRtcConnection);
    procedure HttpClient1ConnectError(Sender: TRtcConnection; E: Exception);
    procedure HttpClient1ConnectFail(Sender: TRtcConnection);
    procedure Btnx_3Click(Sender: TObject);
    procedure DataRequest1DataReceived(Sender: TRtcConnection);
  private
    function  PrepareData:string;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Unit2,Class_User;

{$R *.fmx}

procedure TForm1.Btnx_1Click(Sender: TObject);
begin
  ViewEditUser;
end;

procedure TForm1.Btnx_2Click(Sender: TObject);
begin
  with HttpClient1 do
  begin
    ServerAddr:='192.168.0.51';
    ServerPort:='8181';
    if isConnecting then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with DataRequest1 do
  begin
    Request.Method  :='POST';
    Request.FileName:='/tuser.updatedb?test=helloworld';
    Request.Host       :=HttpClient1.ServerAddr;
    //#Request.ContentType:='application/x-www-form-urlencoded';
    Request.ContentType:='multipart/form-data';
    Request.Params['DATA0']:=PrepareData;
    Request.Params['DATA1']:='158857';
    Request.Params['DATA2']:='158858';
    Request.Params['DATA3']:='158859=golangwillbeerror';
    Post();
  end;
end;

procedure TForm1.Btnx_3Click(Sender: TObject);
begin
  self.IdHTTP1.Get('http://192.168.0.51:8181/tuser.insertdb?data=helloworld',nil);
end;

procedure TForm1.DataRequest1BeginRequest(Sender: TRtcConnection);
var
  DataClient:TRtcDataClient absolute Sender;
begin
  with DataClient do
  begin
    Write(Request.Params.Text);
    //WriteHeader();
  end;
end;

procedure TForm1.DataRequest1DataReceived(Sender: TRtcConnection);
var
  DataClient:TRtcDataClient absolute Sender;
begin
  with DataClient do
  begin
    if Response.Done then
    begin
      ShowMessageFmt('%S',[Read]);
    end;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with Grid_Data do
  begin
    AddCheckBox(1,1,False);
    Cells[2,1]:='HELLOWORLD';

    Grid_Data.Columns.Add;
    Grid_Data.Columns.Add;
    Grid_Data.Columns.Add;
  end;
end;

procedure TForm1.Grid_DataCanFocus(Sender: TObject; var ACanFocus: Boolean);
begin
  ACanFocus:=False;
end;

procedure TForm1.Grid_DataCellClick(Sender: TObject; ACol, ARow: Integer);
begin
  //
end;

procedure TForm1.HttpClient1ConnectError(Sender: TRtcConnection; E: Exception);
begin
  ShowMessage('EROR');
end;

procedure TForm1.HttpClient1ConnectFail(Sender: TRtcConnection);
begin
  ShowMessage('FAIL');
end;

function TForm1.PrepareData: string;
var
  User:TUSER;
  JSON:TQJson;
begin
  Result:='';
  User:=TUSER.Create;
  User.UNITLINK:='0001-0001';
  User.USERINDX:=1;
  User.USERCODE:='0001';
  User.USERNAME:='<username>';
  User.USERPSWD:='<userpswd>';
  User.USERMEMO:='<usermemo>';


  JSON:=TQJson.Create;
  JSON.FromRtti(User);

  Result:=JSON.Value;

  FreeAndNil(JSON);
  FreeAndNil(User);
end;

end.
